package com.divine.wn.car.admin.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.divine.wn.car.admin.persistence.MaintainVehicleMapper;
import com.divine.wn.car.admin.vo.AreaVO;
import com.divine.wn.car.admin.vo.BackUserVO;
import com.divine.wn.car.admin.vo.RoleVO;
import com.divine.wn.car.admin.vo.UserVO;
import com.divine.wn.car.admin.vo.VehicleVO;

/**
 * 
 * @author Administrator
 * 
 */
@Service
public class MaintainVehicleService {
	@Autowired
	private MaintainVehicleMapper maintainVehicleMapper;

	// @Transactional 在需要事务的时候加上这个标签。

	@Transactional
	public void updateTC(HashMap<String, Object> tcMap) {
		maintainVehicleMapper.updateTC(tcMap);
		String[] car = { "dygzfx", "sxf", "dygzfd", "dbf", "cdfwf1", "jgfd","bzj", "ys",  "cdfwf2", "cdfwf3", "jgfx","gpsf1","gpsf2","gpsf3" };
		String[] car_fl = { "dygzfl", "sxfl", "dygzfl", "dbfl", "cdfwfl", "jgfl","bzjl", "ysl", "cdfwfl","cdfwfl","jgfl","gpsfl","gpsfl","gpsfl"};

		for (int i = 0; i < car.length; i++) {
			HashMap<String, Object> tcCarMap = new HashMap<String, Object>();
			tcCarMap.put("costid", i + 1);
			tcCarMap.put("rate", tcMap.get(car[i]));
			tcCarMap.put("tcid", tcMap.get("tcid"));
			tcCarMap.put("cost_fl", tcMap.get(car_fl[i]));
			maintainVehicleMapper.updateTCCar(tcCarMap);
		}

	}

	@Transactional
	public void insertTC(HashMap<String, Object> tcMap) {
		maintainVehicleMapper.insertTC(tcMap);
		int tcid = maintainVehicleMapper.getKey();
		String[] car = { "dygzfx", "sxf", "dygzfd", "dbf", "cdfwf1", "jgfd","bzj", "ys",  "cdfwf2", "cdfwf3", "jgfx","gpsf1","gpsf2","gpsf3" };
		String[] car_fl = { "dygzfl", "sxfl", "dygzfl", "dbfl", "cdfwfl", "jgfl","bzjl", "ysl", "cdfwfl","cdfwfl","jgfl","gpsfl","gpsfl","gpsfl"};


		for (int i = 0; i < car.length; i++) {
			HashMap<String, Object> tcCarMap = new HashMap<String, Object>();
			tcCarMap.put("costid", i + 1);
			tcCarMap.put("rate", tcMap.get(car[i]));
			tcCarMap.put("tcid", tcid);
			tcCarMap.put("cost_fl", tcMap.get(car_fl[i]));
			
			maintainVehicleMapper.insertTCCar(tcCarMap);
		}

	}

	@Transactional
	public void deleteTC(HashMap<String, Object> tcMap) {
		maintainVehicleMapper.deleteTC(tcMap);
		maintainVehicleMapper.deleteTC_Car(tcMap);
	}

	public List<HashMap<String, Object>> getTC() {
		List<HashMap<String, Object>> tcRealList = new ArrayList<HashMap<String, Object>>();
		List<HashMap<String, Object>> tcList = maintainVehicleMapper.getTC();
		for (int i = 0; i < tcList.size(); i++) {
			HashMap<String, Object> tcMap = tcList.get(i);
			List<HashMap<String, Object>> tcCarList = maintainVehicleMapper.getTCCar(tcMap.get("id").toString());
			String[] car = { "dygzfx", "sxf", "dygzfd", "dbf", "cdfwf1", "jgfd","bzj", "ys", "cdfwf2", "cdfwf3", "jgfx","gpsf1","gpsf2","gpsf3" };
			String[] car_fl = { "dygzfl", "sxfl", "dygzfl", "dbfl", "cdfwfl", "jgfl","bzjl", "ysl", "cdfwfl","cdfwfl","jgfl","gpsfl","gpsfl","gpsfl"};

			for (int j = 0; j < tcCarList.size(); j++) {
				tcMap.put(car[j], tcCarList.get(j).get("rate"));
				tcMap.put(car_fl[j], tcCarList.get(j).get("cost_fl"));
			}
			tcRealList.add(tcMap);
		}

		return tcRealList;
	}

	List<HashMap<String, Object>> getTCCar(String tcid) {
		return maintainVehicleMapper.getTCCar(tcid);
	}

	@Transactional
	public void updateVehicle(List<VehicleVO> vehicleList) {
		for (int i = 0; i < vehicleList.size(); i++) {
			maintainVehicleMapper.updateVehicle(vehicleList.get(i));
		}

	}
	
	@Transactional
	public void updateTx(HashMap<String ,Object> map) {
		maintainVehicleMapper.updateTx(map);
	}
	

	// 获取车辆类型
	public List<VehicleVO> getVehicleTypeList() {
		return maintainVehicleMapper.getVehicleTypeList();
	}
	
	public HashMap<String,Object> getTxList() {
		return maintainVehicleMapper.getTxList();
	}

	public List<AreaVO> getAreaDataList() {
		return maintainVehicleMapper.getAreaDataList();
	}

	public int deleteArea(String areaID) {
		return maintainVehicleMapper.deleteArea(areaID);
	}

	@Transactional
	public void updateArea(AreaVO areaVO) {
		maintainVehicleMapper.updateArea(areaVO);
	}

	@Transactional
	public void insertArea(AreaVO areaVO) {
		maintainVehicleMapper.insertArea(areaVO);
	}
	
	public List<RoleVO> getRoleDataList() {
		return maintainVehicleMapper.getRoleDataList();
	}
	
	@Transactional	
	public int deleteRole(String roleID) {
		return maintainVehicleMapper.deleteRole(roleID);
	}

	@Transactional
	public void updateRole(RoleVO RoleVO) {
		maintainVehicleMapper.updateRole(RoleVO);
	}

	@Transactional
	public void insertRole(RoleVO RoleVO) {
		maintainVehicleMapper.insertRole(RoleVO);
	}

	public boolean isExistUserNum(BackUserVO backUserVO) {

		if (maintainVehicleMapper.isExistUserNum(backUserVO) != 0) {
			return true;
		}

		return false;
	}

	public List<HashMap<String, Object>> getUser(HashMap<String, Object> map) {
		return maintainVehicleMapper.getUser(map);
	}

	public int getUserCount(HashMap<String, Object> map) {
		return maintainVehicleMapper.getUserCount(map);
	}

	@Transactional
	public void updateUser(BackUserVO backUserVO) {
		maintainVehicleMapper.updateUser(backUserVO);
		

	}

	@Transactional
	public void deleteUser(BackUserVO backUserVO) {
		maintainVehicleMapper.deleteUser(backUserVO);
	}

	@Transactional
	public void submitUser(BackUserVO backUserVO) {
		maintainVehicleMapper.submitUser(backUserVO);
		
	}

	public int getDataCount(HashMap<String, Object> map) {
		return maintainVehicleMapper.getDataCount(map);
	}

	public List<HashMap<String, Object>> getDataList(HashMap<String, Object> map) {
		return maintainVehicleMapper.getDataList(map);
	}

	@Transactional
	public void deleteDataUser(int id) {
		maintainVehicleMapper.deleteDataUser(id);
		maintainVehicleMapper.deleteDataLog(id);
		maintainVehicleMapper.deleteDataWorkflow(id);
	}

	public List<HashMap<String, Object>> getYWY() {
		return maintainVehicleMapper.getYWY();
	}

	public UserVO getFrontUserInfo(int id) {
		return maintainVehicleMapper.getFrontUserInfo(id);
	}

	public String getXDY(int id) {
		return maintainVehicleMapper.getXDY(id);
	}

	public List<HashMap<String, Object>> getPay(String tableid) {
		return maintainVehicleMapper.getPay(tableid);
	}

	public List<HashMap<String, Object>> getChartData(
			HashMap<String, Object> map) {
		return maintainVehicleMapper.getChartData(map);
	}

}
